package com.stx.openeyes.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.stx.openeyes.MainActivity;
import com.stx.openeyes.R;
import com.stx.openeyes.utils.NToast;
import com.stx.openeyes.view.fragment.MyFragment;
import com.stx.openeyes.widget.ClearWriteEditText;
import com.stx.openeyes.widget.LoadDialog;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HelpActivity extends AppCompatActivity implements View.OnClickListener{
    private final static String TAG = "HelpActivity";
    private static final int CHECK_PHONE = 1;
    private static final int SEND_CODE = 2;
    private static final int VERIFY_CODE = 3;
    private static final int REGISTER = 4;
    private static final int REGISTER_BACK = 1001;
    private ImageView mImgBackground;
    private ClearWriteEditText mPhoneEdit, mAdviceEdit;
    private Button mConfirm;
    private String mPhone, mAdvice;
    protected Context mContext;
    Intent getIntent;
    private Gson mGson;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help);
        mContext = this;
        getIntent = getIntent();
        initView();
        setToolBar();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    private void setToolBar() {
        Toolbar tb = (Toolbar) findViewById(R.id.help_tool);
        setSupportActionBar(tb);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setDisplayShowTitleEnabled(false);
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        tb.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        TextView titleTv = (TextView) findViewById(R.id.help_title);
        String title = getIntent.getStringExtra("title");
        titleTv.setText(title);
    }

    private void initView() {
        mPhoneEdit = (ClearWriteEditText) findViewById(R.id.help_phone);
        mAdviceEdit = (ClearWriteEditText) findViewById(R.id.help_advice);
        mConfirm = (Button) findViewById(R.id.help_button);

        mConfirm.setOnClickListener(this);

//        TextView goLogin = (TextView) findViewById(R.id.reg_login);
//        goLogin.setOnClickListener(this);

        mImgBackground = (ImageView) findViewById(R.id.help_img_backgroud);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Animation animation = AnimationUtils.loadAnimation(HelpActivity.this, R.anim.translate_anim);
                mImgBackground.startAnimation(animation);
            }
        }, 10);

        addEditTextListener();
    }

    private void addEditTextListener() {
        mPhoneEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mAdviceEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 5) {
                    mConfirm.setClickable(true);
                    mConfirm.setBackgroundDrawable(getResources().getDrawable(R.drawable.rs_select_btn_blue));
                } else {
                    mConfirm.setClickable(false);
                    mConfirm.setBackgroundDrawable(getResources().getDrawable(R.drawable.rs_select_btn_gray));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.reg_login:
//                finish();
//                //startActivity(new Intent(this, LoginActivity.class));
//                break;
            case R.id.help_button:
                mPhone = mPhoneEdit.getText().toString().trim();
                mAdvice = mAdviceEdit.getText().toString().trim();


                if (TextUtils.isEmpty(mPhone)) {
                    NToast.shortToast(mContext, getString(R.string.phone_number_is_null));
                    mPhoneEdit.setShakeAnimation();
                    return;
                }
                if (mPhone.contains(" ")) {
                    NToast.shortToast(mContext, "手机号不能有空格");
                    mPhoneEdit.setShakeAnimation();
                    return;
                }
                if (TextUtils.isEmpty(mAdvice)) {
                    NToast.shortToast(mContext, "建议不可为空");
                    mAdviceEdit.setShakeAnimation();
                    return;
                }

                LoadDialog.show(mContext);
                NToast.shortToast(mContext, "反馈成功");
                startActivity(new Intent(mContext, MainActivity.class));
                break;
        }
    }
    @Override
    public void onBackPressed() {
        finish();
    }
}
